package net.zjitc.controller.client;

import net.zjitc.mapper.OrderItemMapper;
import net.zjitc.pojo.Movie;
import net.zjitc.pojo.OrderItem;
import net.zjitc.pojo.Orders;
import net.zjitc.pojo.User;
import net.zjitc.service.MovieService;
import net.zjitc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
//选座购票
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    MovieService movieService;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderItemMapper orderItemMapper;

    @RequestMapping(value = "/index")
    public String index(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Orders> ordersList = orderService.findOrdersByUid(user.getId());
        model.addAttribute("order",ordersList);
        return "/client/orders";
    }

    //进入结算页面，显示选中的购物车中的商品及收货人信息
    @RequestMapping("/showReceiveInfo")
    public String showReceiveInfo(Model model, HttpSession session, HttpServletRequest request){
//        User user = (User) session.getAttribute("user");
        int movie_id = Integer.parseInt(request.getParameter("movie_id"));
        Movie movie = movieService.findById(movie_id);
        model.addAttribute("movie",movie);
//        List<Cart> cartList = cartService.findCartsByUid(user.getId());
//        model.addAttribute("cartList",cartList);
        return "/client/cinemas";

    }

    //选座购票
    @RequestMapping(value = "/xseats")
    public String xseats(HttpServletRequest request,Model model){
        int movie_id = Integer.parseInt(request.getParameter("movie_id"));
        Movie movie = movieService.findById(movie_id);
        model.addAttribute("movie",movie);
        return "/client/seats";
    }

    /**
     * 创建生成订单
     * @return
     *
     * 这里生成的订单是将当前用户下购物车中的所有商品生成订单
     * 实际（选择购物车条目，生成订单）
     */
    @RequestMapping({"/createOrder","/paymentOrder"})
    public String createOrder(HttpSession session, Model model,Orders orders,HttpServletRequest request){
        User user = (User) session.getAttribute("user");
        if (user == null)
        {
            return "/client/login";
        }
        int movieId = Integer.parseInt(request.getParameter("movie_id"));
        Movie movie = movieService.findById(movieId);
        orders.setUser_id(user.getId());
        orders.setMoney(movie.getPrice());
        int resultCount = orderService.createOrder(orders);
        OrderItem orderItem = new OrderItem();
        orderItem.setMovie_id(movieId);
        orderItem.setOrders_id(orders.getId());
        orderItemMapper.createOrderItems(orderItem);
        model.addAttribute("order",orders);
        return "/alipay/alipay.trade.page.pay";
    }

    @RequestMapping("/payReturn")
    public String payReturn(Model model,String out_trade_no){
        int resultCount =  orderService.updatePayState(out_trade_no,1);
        model.addAttribute("msg","付款成功!!!");
        return "/client/createOrderSuccess";
    }
}
